
using System;
using System.Collections;
using Gnome;
using Gdk;

namespace DiaCanvas
{
	public class Circulo:Figura
	{

		private int _radio;
		
		private int _oldSize;
		
		private CanvasEllipse _fig;
		
		private bool _bControlPoint = false;	
		private bool _bResize = false;
		
		private Gdk.Point _oldPoint;
		
		override protected Gnome.CanvasShape Shape { get { return this._fig; }}
		
		public Circulo(Gdk.Point p, int radio, Gnome.Canvas c):base(c)
		{
			this._point = p;

			this._radio = radio;
			
			int z = -10;
			
			this._points = new Gdk.Point[4];
			this._points.SetValue( new Point(p.X-radio-z,p.Y),0);
			this._points.SetValue( new Point(p.X,p.Y+radio+z),1);
			this._points.SetValue( new Point(p.X+radio+z,p.Y),2);
			this._points.SetValue( new Point(p.X,p.Y-radio-z),3);
			
			this._p = new System.Collections.ArrayList();
		}
		
				
		override protected void Resize(Object obj, EventButton ev)
		{
			double dx = ev.X - this._oldPoint.X;
			double dy = ev.Y - this._oldPoint.Y;
			
					
		}
		
		protected override void CalculateControlPoints(){}
		
		public override string toString() { return "Este es un circulo"; }
		public override FiguraType Type { get { return FiguraType.Circulo; } }
		
		protected override void ChangeColor()
		{
			//Write ("Cambiando Color");
			this._fig.OutlineColor =  ( this._bIn ) ? this.BackColor : this.ForeColor;			
		}
		
		override public void draw()
		{
			Write("Dibujando el Circulo"); 
			
			this._fig = new Gnome.CanvasEllipse(this._canvas.Root());
			this._fig.X1 = this._point.X-this._radio;
			this._fig.Y1 = this._point.Y-this._radio;
			this._fig.X2 = this._point.X+this._radio;
			this._fig.Y2 = this._point.Y+this._radio;
			
			this._fig.CanvasEvent += new Gnome.CanvasEventHandler(CanvasEvent);
						
			this._fig.OutlineColor = this.ForeColor;
			this._fig.FillColor = this.ForeColor;
       		this._fig.WidthPixels = this.BorderSize;
       		this._fig.Show();
       		
       		
       		for ( int i = 0 ; i < 4 ; i++)
			{
				int radio = 2;
				Gnome.CanvasEllipse e =  new Gnome.CanvasEllipse(this._canvas.Root()); 
				this._p.Add(e);
				e.X1 = ((Gdk.Point)this._points[i]).X-radio;
				e.Y1 = ((Gdk.Point)this._points[i]).Y-radio;
				e.X2 = ((Gdk.Point)this._points[i]).X+radio;
				e.Y2 = ((Gdk.Point)this._points[i]).Y+radio;
				
				e.OutlineColor = "#fff";//this.BackColor;
				e.FillColor = "#fff";//this.BackColor;
				
				e.WidthPixels = 5;//this.BorderSize;

				e.CanvasEvent += new Gnome.CanvasEventHandler(CanvasEvent);
				
				e.Hide();
			
				
			}
			
		}
		
	
		public void CanvasEvent(object obj, Gnome.CanvasEventArgs args)
		{

			EventButton ev = new EventButton (args.Event.Handle);
			switch (ev.Type) 
			{
				case EventType.MotionNotify:
					if ( this._bDrag )
					{
						Move(ev);
					}
					break;
					
				case EventType.EnterNotify:
					MouseEnter(obj,args);
					break;
	
					
				case EventType.LeaveNotify:
					MouseLeave(obj,args);
					break;
	
				case EventType.DragEnter:
					Write("Drag Enter");
					break;
					
				case EventType.ButtonPress:
					if ( this._bIn )
					{
						if ( this._bControlPoint )
						{
							this._bResize = true;
							Write("Resizing");
						}
						else
						{
							this._bDrag = true;
							this._oldPoint.X = (int)ev.X;
							this._oldPoint.Y = (int)ev.Y;
						}
					}
					break;
					
				case EventType.ButtonRelease:
					if ( this._bDrag )
					{
						if ( this._bResize )
						{
							int dr = (int)(((Gnome.CanvasEllipse)obj).X1 + 2 - (int)ev.X);
							this._point.X += dr;
							this._point.Y += dr;
							Gnome.Canvas canvas = (Gnome.Canvas)((Gnome.CanvasEllipse)obj).Canvas;
							this._fig.Destroy();
							this.draw();
							this._bResize = false;
							Write("Stop Resizing");
						}
						Move(ev);
					}
					this._bDrag = false;

					break;
			}

		}
		

		
	}
}
